<template>

  <div class="app-container calendar-list-container">

    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="用户名" v-model="listQuery.userName">
      </el-input>
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="评论内容" v-model="listQuery.content">
      </el-input>
      <el-select clearable  @change='handleFilter' class="filter-item" style="width: 130px" v-model="listQuery.isCheck" placeholder="审核状态">
        <el-option label="未审核" value="0"></el-option>
        <el-option label="已审核" value="1"></el-option>
      </el-select>

      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <!--<el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">新增</el-button>-->
      <!--<el-button class="filter-item" type="primary" :loading="downloadLoading"  icon="el-icon-download" @click="handleDownload">导出</el-button>-->
    </div>

    <el-table :data="list.slice((currentPage-1)*pageSize,currentPage*pageSize)" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row >
      <el-table-column align="center" label='排序ID' width="95">
        <template slot-scope="scope">
          {{scope.$index}}
        </template>
      </el-table-column>
      <el-table-column label="用户名" width="130" align="center">
        <template slot-scope="scope">
          {{scope.row.userName}}
        </template>
      </el-table-column>
      <el-table-column label="电话" width="130" align="center">
        <template slot-scope="scope">
          {{scope.row.phone}}
        </template>
      </el-table-column>

      <el-table-column label="评论内容" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.content}}</span>
        </template>
      </el-table-column>

      <el-table-column label="评论时间" width="165" align="center">
        <template slot-scope="scope">
          {{scope.row.time}}
        </template>
      </el-table-column>

      <el-table-column label="点赞数" width="110" align="center">
        <template slot-scope="scope">
          {{scope.row.upvote}}
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="审核状态" width="125" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isCheck | statusFilter">{{scope.row.isCheck === 0 ? '未审核' : '已审核'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column  label="操作" width="200" align="center"  >
        <template slot-scope="scope">
       <!--   <el-button type="primary" @click="handleUpdate(scope.row)" size="mini">审核</el-button>  -->
          <el-button size="mini" type="danger" @click="removeUser(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding-top: 15px;text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page=currentPage
        :page-sizes="[10, 20, 30]"
        :page-size=pageSize
        layout="total, sizes, prev, pager, next, jumper"
        :total=total>
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp"  label-position="right" label-width="80px" style='width: 400px; margin-left:50px;'>

        <el-form-item label="审核状态" prop="isCheck">
          <el-select class="filter-item" prop="enable" v-model="temp.isCheck" placeholder="请选择">
            <el-option
              v-for="item in status"
              :key="item.val"
              :label="item.isCheck"
              :value="item.val">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  @click="dialogFormVisible = false">取消</el-button>
        <el-button :loading="buttonLoading" v-if="dialogStatus=='create'" type="primary" @click="createData">确认</el-button>
        <el-button  :loading="buttonLoading" v-else type="primary" @click="updateData">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>

  .filter-container{
    padding-bottom: 20px;
  }

</style>

<script>
  import { List, Merge } from '../../api/comment'

  import { dels } from '../../api/comment'

  import { List as userList } from '../../api/user'

  export default {

    data() {
      return {
        status: [
          { isCheck: '不通过', val: 0 },
          { isCheck: '通过', val: 1 }
        ],
        userList: '',
        list: [],
        commentList: null,
        listLoading: true,
        buttonLoading: false,
        listQuery: {
          userName: null,
          content: null,
          isCheck: null
        },
        temp: {
          commentId: null,
          userId: '',
          userName: '',
          content: '',
          time: '',
          upvote: '',
          pid: '',
          isCheck: ''
        },
        dialogFormVisible: false,
        dialogStatus: '',
        downloadLoading: false,
        dialogPayVisible: false,
        textMap: {
          update: '编辑评论信息：',
          create: '新增评论信息：'
        },
        rules: {
          commentId: [{ required: true }],
          userId: [{ required: true }],
          userName: [{ required: true }],
          content: [{ required: true }],
          time: [{ required: true }],
          upvote: [{ required: true }],
          pid: [{ required: true }],
          isCheck: [{ required: true }]
        },
        total: 0,
        currentPage: 1,
        pageSize: 10
      }
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          1: 'success',
          // draft: 'gray',
          0: 'danger'
        }
        return statusMap[status]
      }
    },
    created() {
      this.fetchData()
      setInterval(this.fetchData, 5000)
    },
    methods: {
      fetchData() {
        this.listLoading = true
        List(this.listQuery.userName, this.listQuery.content, this.listQuery.isCheck).then(response => {
          this.list = response.data
          this.total = response.data.length
          this.currentPage = 1
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
      },
      handleCurrentChange(val) {
        this.currentPage = val
      },
      initUsers() {
        if (this.userList == null) {
          userList(0, 1).then(res => {
            this.userList = res.data.list
          })
        }
      },
      removeUser: function(user) {
        this.$confirm('确定要删除此评论信息吗?', '提示',
          {
            type: 'warning'
          })
          .then((res) => {
            dels({
              commentId: user.commentId
            })
            this.fetchData()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
          .catch(() => {
            this.$message.info('已取消操作!')
          })
      },
      handleFilter() {
        this.fetchData()
      },
      handleCreate() {
        this.resetTemp()
        this.initUsers()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
      },
      handleUpdate(row) {
        this.initUsers()
        this.temp = Object.assign(this.temp, row)
        console.log(this.temp)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
      },
      handleDownload() {

      },
      createData() {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.buttonLoading = true
            Merge(this.temp).then(res => {
              this.temp = res.data
              this.list.unshift(this.temp)
              this.dialogFormVisible = false
              this.buttonLoading = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
              this.fetchData()
            }).catch(reason => {
              console.log(reason)
              this.buttonLoading = false
            })
          } else {
            console.log('验证失败')
          }
        })
      },
      updateData() {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            this.buttonLoading = true
            console.log(this.temp)
            Merge(this.temp).then(res => {
              this.updateList(res.data)
              this.dialogFormVisible = false
              this.buttonLoading = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
              this.fetchData()
            }).catch(reason => {
              console.log(reason)
              this.buttonLoading = false
            })
          }
        })
      },
      updateList(data) {
        for (const v of this.list) {
          if (data.commentId === v.commentId) {
            const index = this.list.indexOf(v)
            data = Object.assign(v, data)
            this.list.splice(index, 1, data)
            break
          }
        }
      },
      resetTemp() {
        this.temp = {
          commentId: null,
          userId: '',
          userName: '',
          content: '',
          time: '',
          upvote: '',
          pid: '',
          isCheck: ''
        }
      }
    }
  }
</script>
